package com.kunlun.basedata.organize.controller;

import com.kunlun.basedata.organize.model.CompanyModel;
import com.kunlun.basedata.organize.service.ICompanyService;
import com.kunlun.common.model.Page;
import com.kunlun.common.utils.ResponseUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * 部门Controller类
 */
@RestController
@RequestMapping(value = "/company")
public class CompanyController {

    private Logger logger = LogManager.getLogger();

    @Autowired
    private ICompanyService companyService;

    @RequestMapping(value = "/getCompanyPage", method = RequestMethod.GET)
    public Object getCompanyPage(CompanyModel companyModel, int currentPage, int pageSize) {
        try {
            Page<CompanyModel> companys = companyService.getCompanyPage(companyModel, currentPage, pageSize);
            return ResponseUtil.successResponse(companys);
        } catch (Exception e) {
            logger.error("CompanyController getCompanyPage Error: ", e);
            return ResponseUtil.failedResponse("获取部门数据失败！", e.getMessage());
        }
    }

    @RequestMapping(value = "/addCompany", method = RequestMethod.POST)
    public Object addCompany(CompanyModel companyModel) {
        try {
            companyService.addCompany(companyModel);
            return ResponseUtil.successResponse("SUCCESS");
        } catch (Exception e) {
            logger.error("CompanyController addCompany Error: ", e);
            return ResponseUtil.failedResponse("添加部门失败！", e.getMessage());
        }
    }

    @RequestMapping(value = "/updateCompany", method = RequestMethod.POST)
    public Object updateCompany(CompanyModel companyModel) {
        try {
            companyService.updateCompany(companyModel);
            return ResponseUtil.successResponse("SUCCESS");
        } catch (Exception e) {
            logger.error("CompanyController updateCompany Error: ", e);
            return ResponseUtil.failedResponse("更新部门失败！", e.getMessage());
        }
    }

    @RequestMapping(value = "/deleteCompany", method = RequestMethod.POST)
    public Object deleteCompany(String ids) {
        try {
            List<String> list = Arrays.asList(ids.split(","));
            companyService.deleteCompany(list);
            return ResponseUtil.successResponse("SUCCESS");
        } catch (Exception e) {
            logger.error("CompanyController deleteCompany Error: ", e);
            return ResponseUtil.failedResponse("删除部门失败！", e.getMessage());
        }
    }
}
